<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Channels;
use App\Models\Log\LogPolicyOpen;
use Illuminate\Http\Request;
use YQ\YqExtend;

class StartController extends Controller
{
    public function startUp(Request $request)
    {
        $game_id = $request->input('game_id');
        $channel = $request->input('channel');
        $device_num = $request->input('device_num');
        $is_policy_show = 1;
        if ($device_num) {
            $log = LogPolicyOpen::whereRaw("device_num='{$device_num}' AND game_id={$game_id}")->first();
            if ($log && $log->is_agree == 1) {
                $is_policy_show = 0;
            }
        }
        // 如果不是官方渠道，则无需弹出
        if (!in_array($channel, [Channels::CHANNEL_GUANFANG, Channels::CHANNEL_H5])) {
            $is_policy_show = 0;
        }

        return yqok([
            'is_policy_show' => $is_policy_show,
            'policy_url' => config('app.url')."/policy/index/{$game_id}?device_num={$device_num}&channel={$channel}",
            'is_permission_on' => 0,
        ]);
    }

    public function agreePolicy(Request $request)
    {
        $game_id    = $request->input('game_id');
        $device_num = $request->input('device_num');
        $is_agree   = $request->input('is_agree');
        if (!$device_num) {
            $device_num = YqExtend::uniqid32();
        }
        $log = LogPolicyOpen::whereRaw("device_num='{$device_num}' AND game_id={$game_id}")->first();
        if (!$log) {
            $log = new LogPolicyOpen;
            $log->create_time = time();
            $log->game_id = $game_id;
            $log->device_num = $device_num;
            $log->ip = YqExtend::getIP();
        }

        $log->agree_time = time();
        $log->agree_ip = YqExtend::getIP();
        $log->is_agree = $is_agree;
        $log->save();

        return yqok([
            'device_num' => $device_num
        ]);
    }
}
